package com.supermarket.service;

import com.supermarket.entity.ArticleMedia;
import com.supermarket.exception.ArticleException;
import com.supermarket.mapper.ArticleMediaMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

@Service
public class ArticleMediaService {

    @Resource
    private ArticleMediaMapper articleMediaMapper;

    // 媒体文件存储路径
    private static final String UPLOAD_DIR = "uploads/article/";

    /**
     * 添加文章媒体
     *
     * @param articleMedia 文章媒体对象
     * @return 添加成功的媒体ID
     */
    public Integer addArticleMedia(ArticleMedia articleMedia) {
        articleMediaMapper.insertArticleMedia(articleMedia);
        return articleMedia.getId();
    }

    /**
     * 更新文章媒体信息
     *
     * @param articleMedia 文章媒体对象
     * @return 影响的行数
     */
    public int updateArticleMedia(ArticleMedia articleMedia) {
        return articleMediaMapper.updateArticleMedia(articleMedia);
    }

    /**
     * 删除文章媒体
     *
     * @param id 媒体ID
     * @return 影响的行数
     */
    public int deleteArticleMedia(Integer id) {
        return articleMediaMapper.deleteArticleMedia(id);
    }

    /**
     * 根据ID获取文章媒体
     *
     * @param id 媒体ID
     * @return 文章媒体对象
     */
    public ArticleMedia getArticleMediaById(Integer id) {
        return articleMediaMapper.selectArticleMediaById(id);
    }

    /**
     * 根据文章ID获取所有媒体
     *
     * @param articleId 文章ID
     * @return 媒体列表
     */
    public List<ArticleMedia> getMediaByArticleId(Integer articleId) {
        return articleMediaMapper.selectMediaByArticleId(articleId);
    }

    /**
     * 根据文章ID和媒体类型获取媒体
     *
     * @param articleId 文章ID
     * @param mediaType 媒体类型
     * @return 媒体列表
     */
    public List<ArticleMedia> getMediaByArticleIdAndType(Integer articleId, String mediaType) {
        return articleMediaMapper.selectMediaByArticleIdAndType(articleId, mediaType);
    }

    /**
     * 上传文章媒体文件
     *
     * @param articleId  文章ID
     * @param file       媒体文件
     * @param mediaType  媒体类型
     * @param sortOrder  排序顺序
     * @return 上传成功的媒体对象
     */
    @Transactional
    public ArticleMedia uploadMedia(Integer articleId, MultipartFile file, String mediaType, Integer sortOrder) throws IOException {
        if (file.isEmpty()) {
            throw new ArticleException("文件不能为空");
        }

        // 确保上传目录存在
        Path uploadPath = Paths.get(UPLOAD_DIR);
        if (!Files.exists(uploadPath)) {
            Files.createDirectories(uploadPath);
        }

        // 生成唯一文件名
        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID() + fileExtension;

        // 保存文件
        Path filePath = uploadPath.resolve(filename);
        Files.copy(file.getInputStream(), filePath);

        // 创建媒体记录
        ArticleMedia media = new ArticleMedia();
        media.setArticleId(articleId);
        media.setMediaType(mediaType);
        media.setMediaUrl(UPLOAD_DIR + filename);
        media.setSortOrder(sortOrder);

        // 保存到数据库
        articleMediaMapper.insertArticleMedia(media);
        
        return media;
    }

    /**
     * 更新媒体排序
     *
     * @param id 媒体ID
     * @param sortOrder 排序顺序
     * @return 影响的行数
     */
    public int updateMediaSortOrder(Integer id, Integer sortOrder) {
        return articleMediaMapper.updateMediaSortOrder(id, sortOrder);
    }

    /**
     * 根据文章ID删除所有媒体
     *
     * @param articleId 文章ID
     * @return 影响的行数
     */
    public int deleteMediaByArticleId(Integer articleId) {
        return articleMediaMapper.deleteMediaByArticleId(articleId);
    }

    /**
     * 根据媒体类型统计数量
     *
     * @param mediaType 媒体类型
     * @return 媒体数量
     */
    public int countMediaByType(String mediaType) {
        return articleMediaMapper.countMediaByType(mediaType);
    }
} 